/**
 * Copyright (c) 2009, Christian Schneider
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * - Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the names of the authors nor the names of its contributors may
 *    be used to endorse or promote products derived from this software without
 *    specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package com.ruinwesen.patch.metadata;

import static java.lang.System.out;
import static java.lang.System.err;

public class PatchMetadataCommand {

    public static void main(String[] args) throws Exception {
        if (args.length == 0) {
            out.println("PatchMetadataCommand 1.0");
            out.println("(C) 2009 - Christian Schneider");
            printUsage();
            return;
        }
        
        PatchMetadata metadata = new DefaultPatchMetadata();
        for(int optindex=0;optindex<args.length;) {
            String option = args[optindex++];
            if ("--new".equals(option)) {
                metadata = new DefaultPatchMetadata();
            } else if ("--open".equals(option)) {
                String fileName = arg(args, optindex++);
                throw new UnsupportedOperationException("no implemented: --open");
            } else if ("--add-path".equals(option)) {
                Path path = parsePath(arg(args, optindex++));
                metadata.addPath(path);
            } else if ("--set-author".equals(option)) {
                metadata.setAuthor(arg(args, optindex++));
            } else if ("--set-title".equals(option)) {
                metadata.setTitle(arg(args, optindex++));
            } else if ("--set-name".equals(option)) {
                metadata.setName(arg(args, optindex++));
            } else if ("--set-comment".equals(option)) {
                metadata.setComment(arg(args, optindex++));
            } else if ("--print-author".equals(option)) {
                out.println(stringOrEmpty(metadata.getAuthor()));
            } else if ("--print-title".equals(option)) {
                out.println(stringOrEmpty(metadata.getTitle()));
            } else if ("--print-name".equals(option)) {
                out.println(stringOrEmpty(metadata.getName()));
            } else if ("--print-version".equals(option)) {
                out.println(stringOrEmpty(metadata.getVersion()));
            } else if ("--print-comment".equals(option)) {
                out.println(stringOrEmpty(metadata.getComment()));
            } else if ("--print-path-list".equals(option)) {
                for (Path path: metadata.getPaths().values()) {
                    if (path.getName()!=null) {
                        out.print(path.getName());
                    }
                    out.print(',');
                    out.println(path.getPath());
                }
            } else if ("--write".equals(option)) {
                String fileName = arg(args, optindex++);
                throw new UnsupportedOperationException("no implemented: --write");
            } else if ("--stdout".equals(option)) {
                PatchMetadataUtils.writeXML(metadata, out);
                out.flush();
            } else {
                err.println("Unknown argument:"+option);
                System.exit(1);
            }
        }
    }
    
    private static Path parsePath(String str) {
        if (str == null || str.length()==0) {
            return null;
        }
        int idx = str.indexOf(',');
        if (idx<0) {
            throw new IllegalArgumentException(str);
        }
        String name = str.substring(0, idx);
        if (name.length()==0) {
            throw new IllegalArgumentException(str);
        }
        String value = str.substring(idx+1);
        return new Path(name, value);
    }
    
    private static String stringOrEmpty(String str) {
        return str == null ? "" : str;
    }
    
    private static String arg(String[] args, int index) {
        return index<0 || index>=args.length ? null : args[index];
    }
    
    static void printUsage() {
        out.println("Usage:");
        out.println("--open <file>|--new");
        out.println("--add-path name,type,path");
        out.println("--set-author <value>");
        out.println("--set-title <value>");
        out.println("--set-name <value>");
        out.println("--print-author");
        out.println("--print-title");
        out.println("--print-name");
        out.println("--print-version");
        out.println("--print-path-list");
        out.println("--write <file>|--stdout");
    }
    
}
